package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
/**
 * 畅享论坛
 */
public class VsharingBbsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url){
		boolean bRet = false;
		String[] regex = {
				"http://bbs.vsharing.com/.*/[0-9]*-1.html",
				//http://bbs.vsharing.com/Promotion/EQ/1800559-1.html
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		String author = null;
		Integer clickNum=0;
		Integer commentNum=0;

		Document doc = Jsoup.parse(htmltxt);
		title = doc.select("h1.ts").text();
		Element firstDiv = doc.select("div#postlist>div").first();
		if(firstDiv!=null){
			Element firstA = firstDiv.select("div.authi a.xw1").first();
			if (firstA!=null){author =firstA .text();}
			date = DateUtils.matchDate(firstDiv.select("div.authi").text());
			content = HtmlCleaner.getContentHtml(url,  firstDiv.select("div.pct"));
		}
		String text = doc.select("div.hm").text();
		clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(text, "|"));
		commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(text, "|"));
		if(StringUtils.isBlank(title)){
		}
		bbs.setClickNum(clickNum);
		bbs.setCommentNum(commentNum);
		bbs.setTitle(StringUtils.trimSpace(title));
		bbs.setContent(content);
		bbs.setDate(date);
		bbs.setAuthor(author);
		bbs.setUpdateUrl(url);
		return bbs;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}
    
	
}
